package Foundation;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.GregorianCalendar;
import java.util.List;

import DomainPackage.Pilot;
import DomainPackage.SatisElemani;
import TechnicalServices.DataConnector;
import TechnicalServices.IMapper;
import DomainPackage.*;

public class MusteriMapper implements IMapper {

	@Override
	public Object get(int ID) {
		Musteri musteri = new Musteri(-1);
		ResultSet rs = DataConnector.getInstance().query("select * from musteri where musteriid = " + ID + "");
		
		if (rs != null){
            
            try {
                    
                    
            		rs.next();
                    musteri.setMusteriID(Integer.parseInt(rs.getString(1)));
                    musteri.setAdi(rs.getString(2));
                    musteri.setSoyadi(rs.getString(6));
                    musteri.setMusteriTipi(Integer.parseInt(rs.getString(5)));
                    musteri.setDogumTarihi(new GregorianCalendar(Integer.parseInt(rs.getString(3).split("-")[0]), 
										Integer.parseInt(rs.getString(3).split("-")[1]), 
										Integer.parseInt(rs.getString(3).split("-")[2])));
            		
                    return musteri;
                   
            } catch (NumberFormatException e) {
                    System.out.println("MusteriMapper: number format problem");
                    e.printStackTrace();
            } catch (SQLException e) {
                    System.out.println("MusteriMapper: sql problem");
                    e.printStackTrace();
            }
    }
    else
            System.out.println("no musteri with given ID");
    return null;
		
	}

	@Override
	public int put(Object object) {
		
		Musteri musteri=(Musteri) object;
			String dt = "";
			GregorianCalendar dta = musteri.getDogumTarihi();
			dt += String.valueOf(dta.get(GregorianCalendar.YEAR)) + "-";
			dt += String.valueOf(dta.get(GregorianCalendar.MONTH)) + '-';
			dt += String.valueOf(dta.get(GregorianCalendar.DAY_OF_MONTH));
			if(!DataConnector.getInstance().update("INSERT INTO musteri "+
					"(musteriid, adi, dogumtarihi, indirimmiktari, musteritipi, soyadi) " +
					"values ('"+musteri.getMusteriID()+"', '"+
					musteri.getAdi()+"', '"+
					
					dt+"', '"+
					musteri.getIndirimMiktari()+"', '"+
					musteri.getMusteriTipi()+"', '"+
					musteri.getSoyadi()+"')")){
				System.out.println("error");
			}
		
		 return 0;
	}

	@Override
	public List<Object> getAll() {
		return null;
	}

}
